package com.vvhard.demo.controller;

import com.sf.ms.wp.context.ExcelExportContext;
import com.sf.ms.wp.context.Result;
import com.sf.ms.wp.entity.Task;
import com.sf.ms.wp.example.ExportDTO;
import com.sf.ms.wp.example.Order;
import com.sf.ms.wp.example.OrderService;
import com.sf.ms.wp.export.ExcelExport;
import com.sf.ms.wp.support.ExcelExportTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author zhangqirong
 * @Date 2022/1/5
 */
@Slf4j
@RestController
public class TestController {

    @GetMapping("/test")
    public  Result<String> test() {
        final OrderService orderService = new OrderService();
        ExcelExportContext context = new ExcelExportContext("01397336", "导出.xls", Order.class);
        context.setTempDir((String)null);
        context.setClearTempFiles(false);
        ExcelExportTask excelExportTask = new ExcelExportTask<ExportDTO, Order>(context, (ExportDTO)null) {
            @Override
            public int count(ExportDTO params) {
                return orderService.count(params);
            }

            @Override
            public List<Order> listByPage(ExportDTO params, Integer pageNum, Integer pageSize, Long lastId) {
                return orderService.list(params, pageNum, pageSize);
            }
        };
        Result<String> result = ExcelExport.execute(excelExportTask);
        return result;
    }

    @PostMapping("/create")
    public  Result<String> test1(@RequestBody Task task) {
        log.info(task.getFileName());
        return Result.success();
    }

    @PostMapping("/complete")
    public  Result<String> test2(@RequestParam(value = "file", required = false) MultipartFile file, Task task) {
        String originalFilename = file.getOriginalFilename();
        try {
            if (file != null) {
                file.transferTo(new File(originalFilename));
                log.info("保存成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success();
    }
}
